<template>
  <div>
    <lan-header :config="headerConfig"></lan-header>
    <lan-search-form
      size="small"
      :config="searchForm"
      :formData="searchFormData"
      @search="search"
    ></lan-search-form>
    <add
      ref="addComponent"
      :organizationList="organizationList"
      :organizationTotalList="organizationTotalList"
      @search="search"
    ></add>
    <upload
      ref="uploadComponent"
      @search="search"
    ></upload>
  </div>
</template>

<script>
import Add from './add';
import Upload from './upload';
import lanSearchForm from '../../../components/lanSearchForm';
import lanHeader from '../../../components/lanHeader';
import moment from 'moment';
import { MomentFormat, SortType } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { deleteAreaApi, getAreaListApi } from '../../../api/area';
import { parseError } from '../../../utils';

export default {
  components: {
    lanHeader,
    lanSearchForm,
    Add,
    Upload
  },
  props: {
    searchFormData: {
      type: Object,
      default() {
        return {};
      }
    },
    organizationList: {
      type: Array,
      default: () => []
    },
    organizationTotalList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      headerConfig: {
        title: '用户列表',
        buttons: [
          {
            text: '新建',
            icon: 'add',
            type: 'primary',
            size: 'small',
            method: () => {
              this.showAdd();
            }
          },
          {
            text: '批量导入',
            icon: 'add',
            type: 'primary',
            size: 'small',
            method: () => {
              this.showUpload();
            }
          }
        ]
      },

      searchForm: {
        inline: true,
        size: 'small',
        formList: [
          {
            name: 'name',
            type: 'input',
            placeholder: '账号/用户名'
          },
          {
            name: 'organizationId',
            placeholder: '组织',
            type: 'select',
            options: () => this.organizationList,
            optionValue: 'id',
            optionLabel: 'name'
          },
          {
            name: 'email',
            type: 'input',
            placeholder: '邮箱'
          },
          {
            name: 'mobile',
            type: 'input',
            placeholder: '手机号'
          }
        ]
      }
    };
  },
  methods: {
    search() {
      this.$emit('search');
    },
    showAdd() {
      this.$refs.addComponent.showDialog();
    },
    // refresh() {
    //   this.$emit('refresh');
    // },
    showUpload() {
      this.$refs.uploadComponent.showDialog();
    },
    add() {
      createAreaApi(this.addFormData.name)
        .then(res => {
          Message.success('添加成功');
          this.dialogVisible = false;
          this.$emit('refresh');
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => {
          this.search();
        });
    }
  }
};
</script>

<style>
</style>
